package com.medical.admin.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.sys.common.dto.RoleDto;
import com.medical.admin.sys.service.RoleService;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.response.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/*
 * @Date : 2024/4/26 16:34
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Api(tags = "角色管理接口文档")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @PostMapping("/add")
    @ApiOperation("添加角色")
    public ResponseEntity<String> addRole(@Valid @RequestBody RoleDto roleDto) {
        Integer i = roleService.addRole(roleDto);
        if (i > 0){
            return ResponseEntity.success(ResponseCode.ROLE_ADD_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ROLE_ADD_ERROR);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询角色")
    public ResponseEntity<Page<RoleDto>> listRole(String roleName, @RequestParam(defaultValue = "1",required = false) Integer pageNum, @RequestParam(defaultValue = "5",required = false) Integer pageSize) {
        return ResponseEntity.success(roleService.list(roleName, pageNum, pageSize));
    }

    @PutMapping("/modStatus")
    @ApiOperation("切换状态")
    public ResponseEntity<String> modStatus(@RequestParam("roleId")Integer roleId,Integer roleStatus) {
        Integer i = roleService.modStatusById(roleId, roleStatus);
        if (i > 0){
            return ResponseEntity.success(ResponseCode.ROLE_MOD_STATUS_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ROLE_MOD_STATUS_ERROR);
    }

    @PostMapping("/mod")
    @ApiOperation("修改角色")
    public ResponseEntity<String> mod(@Valid @RequestBody RoleDto roleDto) {
        Integer mod = roleService.mod(roleDto);
        if (mod > 0){
            return ResponseEntity.success(ResponseCode.ROLE_MOD_ROLE_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.ROLE_MOD_ROLE_ERROR);
    }

    @PutMapping("/delBatch")
    @ApiOperation("批量删除角色")
    public ResponseEntity<String> delBatch(@RequestBody Integer[] roleIds) {
        Integer i = roleService.delBatch(roleIds);
        if (i > 0){
            return ResponseEntity.success(ResponseCode.ROLE_DEL_BATCH_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.ROLE_DEL_BATCH_ERROR.getMsg());
    }

    @PutMapping("/del")
    @ApiOperation("删除角色")
    public ResponseEntity<String> del(@RequestParam("roleId")Integer roleId) {
        Integer del = roleService.del(roleId);
        if (del > 0){
            return ResponseEntity.success(ResponseCode.ROLE_DEL_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.ROLE_DEL_ERROR.getMsg());
    }

    @GetMapping("/find")
    @ApiOperation(value = "根据id查询角色")
    public ResponseEntity<RoleDto> find(@RequestParam("roleId")Integer roleId) {
        RoleDto roleDto = roleService.selectById(roleId);
        if (ObjectUtil.isNotEmpty(roleDto)){
            return ResponseEntity.success(roleDto);
        }
        return ResponseEntity.error(ResponseCode.ROLE_FIND_ERROR);
    }
}
